package com.zztl.infiniteBand.ui.view.dialog

import android.app.Dialog
import android.content.Context
import android.support.annotation.StyleRes
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView

import com.zztl.infiniteBand.R


/**
 * @desc
 * @auth ${user}
 * @time 2018/1/9  15:12
 */

class MentionDialog : Dialog {
    private var mDismissListener: DismissListener? = null

    class Builder(private val context: Context) {
        private var message: String? = null
        private var money: String? = null
        private var charge: String? = null
        private var reveiverNum: String? = null
        private var isShowMessage = true
        private var isCancelable = false
        private var isCancelOutside = false
        private var affirmListener: ClickListener? = null
        private var cancelListener: ClickListener? = null
        private var getCodeListener: ClickListener? = null
        private var affirmTxt: String? = null
        private var cancelTxt: String? = null
        private var btnColor = -1
        private var mDismissListener: DismissListener? = null

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        fun setMoney(money: String): Builder {
            this.money = money
            return this
        }


        fun setCharge(charge: String): Builder {
            this.charge = charge
            return this
        }


        fun setReceiverNum(reveiverNum: String): Builder {
            this.reveiverNum = reveiverNum
            return this
        }


        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        fun setShowMessage(isShowMessage: Boolean): Builder {
            this.isShowMessage = isShowMessage
            return this
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }

        fun setClickListener(listener: ClickListener): Builder {
            this.affirmListener = listener
            this.cancelListener = listener
            this.getCodeListener = listener
            return this
        }

        /*  public Builder setCancelListener(ClickListener cancelListener) {
              this.cancelListener = cancelListener;
              return this;
          }
  */
        fun setAffirmTxt(affirmTxt: String): Builder {
            this.affirmTxt = affirmTxt
            return this
        }

        fun setCancelTxt(cancelTxt: String): Builder {
            this.cancelTxt = cancelTxt
            return this
        }

        fun setBtnColor(color: Int): Builder {
            this.btnColor = color
            return this
        }

        fun setDismissListener(dismissListener: DismissListener): Builder {
            mDismissListener = dismissListener
            return this
        }

        fun create(): MentionDialog {

            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.layout_dialog_mention, null)
            val dialog = MentionDialog(context, R.style.CommomDialog)

            val msgText = view.findViewById<TextView>(R.id.title)
            val moneyTxt = view.findViewById<TextView>(R.id.tv_money)
            val chargeTxt = view.findViewById<TextView>(R.id.tv_charge)
            val receiverNumTxt = view.findViewById<TextView>(R.id.tv_reveicer_number)
            val etPwd = view.findViewById<EditText>(R.id.et_trans_pwd)
//            val etPwd = view.findViewById<EditText>(R.id.et_trans_pwd).apply {
//                inputType=InputType.TYPE_TEXT_VARIATION_PASSWORD
//                setSingleLine(false)
//                setHorizontallyScrolling(false)
//            }
            val etCode = view.findViewById<EditText>(R.id.et_code)
//            val etCode = view.findViewById<EditText>(R.id.et_code).apply {
//                inputType=InputType.TYPE_CLASS_NUMBER
//                setSingleLine(false)
//                setHorizontallyScrolling(false)
//            }
            btnCode = view.findViewById(R.id.btn_code)
            val submitBtn = view.findViewById<TextView>(R.id.submit)
            val cancelBtn = view.findViewById<TextView>(R.id.cancel)
            if (null != affirmTxt) {
                submitBtn.text = affirmTxt
            }
            if (null != cancelTxt) {
                cancelBtn.text = cancelTxt
            }

            if (-1 != btnColor) {
                submitBtn.setTextColor(context.resources.getColor(btnColor))
                cancelBtn.setTextColor(context.resources.getColor(btnColor))
            }

            submitBtn.setOnClickListener {
//                dialog.dismiss()
                if (null != affirmListener) {
                    affirmListener!!.comfirm(etPwd.text.toString().trim { it <= ' ' }, etCode.text.toString().trim { it <= ' ' })
                }
            }


            cancelBtn.setOnClickListener {
                dialog.dismiss()
                if (null != cancelListener) {
                    cancelListener!!.cancel()
                }
            }

            btnCode!!.setOnClickListener {
                if (null != getCodeListener) {
                    getCodeListener!!.getCode()
                }
            }

            if (isShowMessage) {
                msgText.text = message
            } else {
                msgText.visibility = View.GONE
            }
            moneyTxt.text = money
            chargeTxt.text = charge
            receiverNumTxt.text = reveiverNum
            dialog.setContentView(view)
            dialog.setCancelable(isCancelable)
            dialog.setCanceledOnTouchOutside(isCancelOutside)
            dialog.setDismissListener(mDismissListener)
            return dialog

        }


        interface ClickListener {
            fun cancel()

            fun comfirm(pwd: String, code: String)

            fun getCode()
        }


    }

    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, @StyleRes themeResId: Int) : super(context, themeResId) {

    }

    fun showDialog() {
        val window = this.window
        val lp = window!!.attributes
        /*
         * 设置宽高，不设置默认根据内容自适应
         */
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = lp
        show()
    }

    override fun dismiss() {
        if (null != mDismissListener) {
            mDismissListener!!.dismiss()
        }
        super.dismiss()
    }

    fun setDismissListener(dismissListener: DismissListener?) {
        this.mDismissListener = dismissListener
    }

    fun setBtnTxt(txt: String) {
        btnCode!!.text = txt
    }

    fun setBtnColor(color: Int) {
        btnCode!!.setTextColor(color)
    }

    fun setBtnUsable(usable: Boolean) {
        btnCode!!.isEnabled = if (usable) true else false
    }

    companion object {


        private var btnCode: TextView? = null
    }
}
